package com.qckj.amz.domain.spapi.feeds.home.RelationshipFeedXml;

import com.qckj.amz.domain.ProductFmlBasic;
import com.qckj.amz.domain.ProductFmlRlat;
import com.qckj.amz.domain.ProductInit;
import com.qckj.amz.domain.spapi.feeds.home.AmazonEnvelope;
import com.qckj.amz.domain.spapi.feeds.home.Header;
import com.qckj.amz.domain.spapi.feeds.home.Message;
import com.qckj.amz.utils.constants.AmzConstants;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * 创建价格xml
 * @author luchuan
 * @version v1.0
 * @date 2022/11/20 0020
 *
 **/

public class CreateRelationshipFeedXml {


    /**
     *
     * @param sellerId      卖家记号
     * @param productInit   产品常用属性
     * @param listProduct   产品信息
     * @return
     */
    public String getRelationshipFeedXml(String sellerId, ProductInit productInit, List<Map<String, Object>> listProduct) {

        if (listProduct==null && listProduct.isEmpty()){
            return null;
        }

        ArrayList<Message> messageList = new ArrayList<>();//封装多个Message，即多个sku产品，无论是有无变体或多个不同的产品
        for (Map<String, Object> mapProduct: listProduct) {
            ProductFmlBasic basicObj = (ProductFmlBasic)mapProduct.get("basicObj");
            List<ProductFmlRlat> rlatList = (List<ProductFmlRlat>)mapProduct.get("rlatList");
            //封装多个变体
            for (ProductFmlRlat rlat:rlatList) {

                String parentChild = rlat.getParentChild()==null ? "" : rlat.getParentChild();
                if (parentChild.equals(AmzConstants.PARENTAGE_CHILD)){
                    ArrayList<Relation> relationList = new ArrayList<>();
                    Relation relation = new Relation();
                    relation.setSKU(rlat.getItemSku());
                    relation.setType("Variation");
                    relationList.add(relation);

                    Relationship relationship = new Relationship();
                    relationship.setParentSKU(rlat.getParentSku()==null ? "" : rlat.getParentSku());
                    relationship.setRelation(relationList);

                    /**
                     * 二级/Message,此节点为List对象，可以包含主体、变体
                     */
                    Message message = new Message();
                    message.setMessageID(basicObj.getId());
                    message.setOperationType(productInit.getUpdateDelete());//Update、delete
                    message.setRelationship(relationship);
                    messageList.add(message);
                }

            }
        }

        // 若messageList大小为0，则无变体
        String xmlStr = "";
        if (messageList.size()!=0){
            /**
             * 二级/Header
             */
            Header header = new Header();
            header.setDocumentVersion("1.01");
            header.setMerchantIdentifier(sellerId);

            /**
             * 一级/根节点AmazonEnvelope
             */
            AmazonEnvelope amazonEnvelope = new AmazonEnvelope();
            amazonEnvelope.setHeader(header);
            amazonEnvelope.setMessageType("Relationship");//Product、Price、Relationship
            amazonEnvelope.setMessage(messageList);

            /**
             * Javabean对象转换xml字符串
             */
            xmlStr = AmzCommon.JavabeanToXmlStr(AmzConstants.FEED_TYPE_RELATIONSHIPS_FEED, amazonEnvelope);
        }
        return xmlStr;
    }
}
